package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.exception.BusinessException;
import com.company.project.entity.AActiveEntity;
import com.company.project.entity.AClassEntity;
import com.company.project.entity.AStudentEntity;
import com.company.project.service.AActiveService;
import com.company.project.service.AClassService;
import com.company.project.service.AStudentService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import com.company.project.common.utils.DataResult;

import com.company.project.entity.APaidRecordEntity;
import com.company.project.service.APaidRecordService;

import javax.validation.Valid;


/**
 * ${comments}
 *
 * @author wenbin
 * @email *****@mail.com
 * @date 2022-06-06 11:32:36
 */
@Controller
@RequestMapping("/")
public class APaidRecordController {
    @Autowired
    private APaidRecordService aPaidRecordService;
    @Autowired
    private AStudentService aStudentService;
    @Autowired
    private AActiveService aActiveService;
    @Autowired
    private AClassService aClassService;


    /**
    * 跳转到页面
    */
    @GetMapping("/index/aPaidRecord")
    public String aPaidRecord() {
        return "apaidrecord/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("aPaidRecord/add")
    @RequiresPermissions("aPaidRecord:add")
    @ResponseBody
    public DataResult add(@RequestBody @Valid APaidRecordEntity aPaidRecord){
        AStudentEntity aStudent = aStudentService.getById(aPaidRecord.getStudentId());
        if(null==aStudent){
            throw new BusinessException("该学生ID不存在");
        }
        AActiveEntity aActive = aActiveService.getById(aPaidRecord.getActiveId());
        if(null==aActive){
            throw new BusinessException("该活动ID不存在");
        }
        //查询该学生在该活动下是否缴费
        LambdaQueryWrapper<APaidRecordEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(APaidRecordEntity::getStudentId, aPaidRecord.getStudentId())
                .eq(APaidRecordEntity::getActiveId, aPaidRecord.getActiveId());
        APaidRecordEntity newAPaidRecord = aPaidRecordService.getOne(queryWrapper);
        if(null!=newAPaidRecord){
            throw new BusinessException("该学生已报名缴费该活动，无需重复报名");
        }
        AClassEntity classEntity = aClassService.getById(aStudent.getClassId());
        aPaidRecord.setStudentName(aStudent.getName());
        aPaidRecord.setActiveName(aActive.getName());
        aPaidRecord.setClassName(classEntity.getName());
        aPaidRecordService.save(aPaidRecord);
        return DataResult.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("aPaidRecord/delete")
    @RequiresPermissions("aPaidRecord:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        aPaidRecordService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("aPaidRecord/update")
    @RequiresPermissions("aPaidRecord:update")
    @ResponseBody
    public DataResult update(@RequestBody @Valid  APaidRecordEntity aPaidRecord){
        AStudentEntity aStudent = aStudentService.getById(aPaidRecord.getStudentId());
        if(null==aStudent){
            throw new BusinessException("该学生ID不存在");
        }
        AActiveEntity aActive = aActiveService.getById(aPaidRecord.getActiveId());
        if(null==aActive){
            throw new BusinessException("该活动ID不存在");
        }
        //查询该学生在该活动下是否缴费
        LambdaQueryWrapper<APaidRecordEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(APaidRecordEntity::getStudentId, aPaidRecord.getStudentId())
                .eq(APaidRecordEntity::getActiveId, aPaidRecord.getActiveId());
        APaidRecordEntity newAPaidRecord = aPaidRecordService.getOne(queryWrapper);
        if(null!=newAPaidRecord){
            throw new BusinessException("该学生已报名缴费该活动，无需重复报名");
        }
        AClassEntity classEntity = aClassService.getById(aStudent.getClassId());
        aPaidRecord.setStudentName(aStudent.getName());
        aPaidRecord.setActiveName(aActive.getName());
        aPaidRecord.setClassName(classEntity.getName());
        aPaidRecordService.updateById(aPaidRecord);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("aPaidRecord/listByPage")
    @RequiresPermissions("aPaidRecord:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody APaidRecordEntity aPaidRecord){
        Page page = new Page(aPaidRecord.getPage(), aPaidRecord.getLimit());
        LambdaQueryWrapper<APaidRecordEntity> queryWrapper = Wrappers.lambdaQuery();
        //查询条件示例
        queryWrapper.like(StringUtils.isNotBlank(aPaidRecord.getStudentName()),APaidRecordEntity::getStudentName, aPaidRecord.getStudentName());
        IPage<APaidRecordEntity> iPage = aPaidRecordService.page(page, queryWrapper);
        return DataResult.success(iPage);
    }

}
